import jwt
from django.conf import settings
import time

#加密
def encrypt(data:dict):
    
    key=settings.JWT_SECRET_KEY
    #添加过期时间
    data.update({'exp':time.time()+settings.JWT_EXPIRE})
    s = jwt.encode(data,key=key,algorithm='HS256')
    
    return s

#解密
def verify_token(token:str):
    key=settings.JWT_SECRET_KEY
    try:
        data = jwt.decode(token, key=key, algorithms=['HS256'])
        return data
    except Exception as e:
        return None

if __name__ == '__main__':
    token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6Inl5eSIsInBhc3N3b3JkIjoiMTIzNCIsImV4cCI6MTcxMTc4NzA0OC4xNTg0ODgzfQ.CHyCnmmDG49Xueb4xlcBVrsUfLUapX6kNm0xOQtS-pQ'
    data1=verify_token(token)
    print(data1)
    # {'username': 'admin', 'password': '<PASSWORD>', 'exp': 1643759028.98376}